package com.jivam.lottery.data;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import com.jivam.lottery.util.Constants;

/**
 * 数据处理类
 * @author Jivam
 */
public class DataUtil {
	private static Map<String, Object> data ;
	
	private DataUtil(){
	}

	/**
	 * 添加数据
	 * @param key 关键字
	 * @param value 数值
	 */
	public static void addData(String key,Object value){
		if(data == null)
			loadData();
		data.put(key, value);
	}
	
	/**
	 * 获取数据
	 * @param key 关键字
	 * @return 
	 * @return
	 */
	public static <T> T getData(String key,T defaultValue){
		T result = getData(key);
		if(result == null)
			return defaultValue;
		return result;
	}
	
	/**
	 * 获取数据
	 * @param key 关键字
	 * @return 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getData(String key){
		if(data == null)
			loadData();
		return (T)data.get(key);
	}
	
	
	/**
	 * 保存数据
	 */
	public static void saveData() {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream(Constants.PATH_DATASAVE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(data);
			oos.flush();
		} catch (Exception e) {
			writeLog(e);
		}finally{
			if(fos!=null)try{fos.close();}catch(IOException e){}
			if(oos!=null)try{oos.close();}catch(IOException e){}
		}
	}
	
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public static void loadData() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(Constants.PATH_DATASAVE);
			ois = new ObjectInputStream(fis);
			data =  (Map<String, Object>)ois.readObject();
		} catch (Exception e) {
			data = new HashMap<String, Object>();
		}finally{
			if(fis!=null)try{fis.close();}catch(IOException e){}
			if(ois!=null)try{ois.close();}catch(IOException e){}
		}
	}
	
	/**
	 * 清空数据
	 */
	public static void clearData() {
		if(data == null)
			loadData();
		data.clear();
	}
	
	/**
	 * 写入错误异常
	 * @param ex
	 */
	public static void writeLog(Exception ex){
		writeLog(null, ex);
	}
	
	/**
	 * 写入错误日志
	 * @param log
	 */
	public static void writeLog(String log){
		writeLog(log, null);
	}
	
	/**
	 * 写入日志
	 * @param log 自定义日志
	 * @param ex 异常
	 */
	public static void writeLog(String log,Exception e){
		FileWriter fw = null;
		PrintWriter pw = null;
		try {
			fw = new FileWriter(Constants.PATH_LOG,true);
			pw = new PrintWriter(fw);
			if(log != null)
				pw.println(log);
			if(e != null)
				e.printStackTrace(pw);
			pw.flush();
		} catch (IOException ex) {
			ex.printStackTrace();
		}finally{
			try{if(pw != null);pw.close();}catch (Exception ex){}
			try{if(fw != null);fw.close();}catch (Exception ex){}
		}
	}
	
	public static void writePeriod(String num){
		FileWriter fw = null;
		PrintWriter pw = null;
		try {
			fw = new FileWriter(Constants.PATH_PERIOD,true);
			pw = new PrintWriter(fw);
			pw.println(num);
			pw.flush();
		} catch (IOException e) {
			writeLog("写入期数异常", e);
		}finally{
			try{if(pw != null);pw.close();}catch (Exception e){}
			try{if(fw != null);fw.close();}catch (Exception e){}
		}
	}

}
